package com.shanghaionstar.discover.geofence;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.GeofenceInfoEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.geofence.iview.IGeofenceHomeView;
import com.shanghaionstar.discover.geofence.presenter.GeofenceHomePresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;

/**
 * Created by 7828wc on 16/7/21.
 */
@ContentView(R.layout.discover_geofence_home)
public class GeofenceHomeActivity extends SwipeActivity implements IGeofenceHomeView,View.OnClickListener {
    @FindViewById
    private ProgressLayout progress_layout;
    @FindViewById
    private CustomLineLayout add_ele_fence_layout;
    private GeofenceHomePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new GeofenceHomePresenter(getScheduler());
        presenter.setView(this);

        presenter.getGeofencing();
    }

    @Override
    public BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    public void setLoading(boolean isLoading) {
        progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e, new AlertDialogUtil.AlertDialogEventProxy() {
            @Override
            public void onPositiveClickListener(DialogInterface dialog) {
                finish();
            }

            @Override
            public void onCancelListener(DialogInterface dialog) {
                finish();
            }
        });
    }

    @Override
    public void onReponseSuccess(GeofenceInfoEntity entity) {
        if (entity != null) {
            Intent intent = new Intent(onActivity(), GeofenceDetailActivity.class);
            presenter.setRule(entity);
            intent.putExtra(GeofenceInfoEntity.GEOFENCE_TAG, entity);
            startActivity(intent);
            finish();
        } else {
            add_ele_fence_layout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public Activity onActivity() {
        return this;
    }

    @OnClick({R.id.add_ele_fence_layout})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_ele_fence_layout:
                presenter.launchGeofenceInfo();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.finish();
        presenter = null;
    }
}
